<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="http://192.168.0.238:8000/CLodopfuncs.js"></script>
    <style>
      .test_text {
        color: red;
        background-color: yellow;
      }
    </style>
  </head>
  <body>
    <div>
      <!-- <span class="test_text" onclick="print">打印内容</span> -->
      <img src="http://47.92.246.66:1112/image/ys.png" class="pt_img" />
    </div>
  </body>
</html>
<script type="text/javascript">
  function print() {}
  let base64ImgP

  const btnEl = document.querySelector('.pt_img')
  btnEl.onclick = function () {
    console.log('点击打印')
    console.log('要打印的图片base64', base64ImgP)

    var strHTML = document.querySelector('.pt_img').innerHTML
    LODOP.PRINT_INITA(1, 1, 770, 660, '测试预览功能')
    LODOP.ADD_PRINT_TEXT(10, 60, 300, 200, '这是测试的纯文本，下面是超文本:')
    LODOP.ADD_PRINT_IMAGE(base64ImgP)
    LODOP.PREVIEW()
  }

  function convertImgToBase64(url, callback, outputFormat) {
    var canvas = document.createElement('CANVAS'),
      ctx = canvas.getContext('2d'),
      img = new Image()
    img.crossOrigin = 'Anonymous'
    img.onload = function () {
      canvas.height = img.height
      canvas.width = img.width
      ctx.drawImage(img, 0, 0)
      var dataURL = canvas.toDataURL(outputFormat || 'image/jpg')
      callback.call(this, dataURL)
      canvas = null
    }
    img.src = url
  }
  //参考链接
  let url = document.querySelector('.pt_img').src
  convertImgToBase64(url, (base64Img) => {
    alert(base64Img) //转化后的base64文件
    base64ImgP = base64Img
  })
</script>
